// pages/Login/index.js
const app = getApp()
const util = require('../../../utils/util.js')
const http = require('../../../utils/http.js');
Page({
  data: {
    code: ''
  },
  //授权登录
  wxLogin(data) {
    var _this = this
    http.postRequest('v1/wxLogin', data,
      function (res) {
        wx.setStorageSync('token', res.data.token)
        getApp().globalData.token = res.data.token;
        if (res.code == 200 || res.code == 201) {
          _this.syncData({
            is_sync: 1
          })
        }
      },
      function (res) {
        if (res.code == 402) {
          wx.redirectTo({
            url: '/pages/Login/Entercode/Entercode',
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
  },
  //缓存数据
  syncData(data) {
    var _this = this
    http.postRequest('syncData', data, function (res) {
        wx.setStorageSync('member', res.data)
        getApp().globalData.token = res.data.token;
        if (res.code == 200) {
          console.log(res)
          if (res.data.member_info != undefined) {
            wx.setStorageSync('renzheng', 1)
          }
          var pages = getCurrentPages();
          console.log(pages[pages.length - 2])
          if (pages[pages.length - 2].route == 'tabbarPage/UserCenter/userCenter') {
            http.getRequest('v1/user', data, function (res) {
              wx.setStorageSync('v1/user', res)
              wx.showToast({
                title: '登录成功',
                icon: 'success'
              })
              setTimeout(() => {
                wx.navigateBack({
                  delta: 1
                })
              }, 1000)
            }, function (err) {})
          }else{
            wx.showToast({
              title: '登录成功',
              icon: 'success'
            })
            setTimeout(() => {
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          }
        }
       },function (res) {
        _this.toasts.hideToast({})
        if (res.code == 402) {
          wx.redirectTo({
            url: '/pages/Login/Entercode/Entercode',
          })
        } 
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    console.log(getApp())
    wx.login({
      success: res => {
        _this.setData({
          code: res.code
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  //跳转输入邀请码
  bindGetUserInfo: util.throttle(function (e) {
    wx.showToast({
      title: '登录中...',
      icon: 'loading',
      duration:10000
    })
    wx.login({
      success: res => {
        const code = res.code;
        const userInfo = e.detail;
        
        userInfo.userInfo.nickName = 'AA'
        var string = JSON.parse(userInfo.rawData) 
        string.nickName = 'AA'
        userInfo.rawData = JSON.stringify(string)
        var stringInfo = JSON.stringify(userInfo)
        
        console.log('传给组件的',stringInfo)
        var info = stringInfo.split(''),
          list = ''
          
        for (let i in info) {
          if (info[i] != ' ') {
            list = list + info[i]
          }
        }
        
        const data = {
          client_data: list,
          code: code
        }
        if (e.detail.userInfo) {
          this.wxLogin(data)
          wx.setStorageSync('Info', JSON.parse(list))
        } else {
          wx.showModal({
            title: '温馨提示',
            content: '请授权'
          })
        }
      }
    })
  }, 1000),

  onReady: function () {
    //获得dialog组件 
    this.toastDialog = this.selectComponent("#toastDialog"); //此处id和页面的 wxml中的id一致
    this.toasts = this.selectComponent('#toasts')
  },
  //返回首页
  back: util.throttle(function (e) {
    wx.switchTab({
      url: '/tabbarPage/index/index',
    })
  }, 1000)
})